package Java关键字;

import java.io.*;

/**
 * @author:谢君臣
 * @Date:2021/9/28-9:49
 * @version:1.0
 * @function:
 */
public class 序列化 {
    // serialVersionUID 防止对象反序列化时，类的版本不一样，所有设定一个final变量

    public static void main(String[] args) throws Exception {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("./test.txt"));
        person p = new person(123,"张三");
        oos.writeObject(p);
        oos.close();
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("./test.txt"));
        person object = (person) ois.readObject();
        System.out.println(object.toString());
    }

}
class person implements Serializable {
    private static final long serialVersionUID = 12334534L;
    private int value;
    private String name;

    public person() {
    }

    public person(int value, String name) {
        this.value = value;
        this.name = name;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "person{" +
                "value=" + value +
                ", name='" + name + '\'' +
                '}';
    }
}